package com.atguigu.eduservice.controller.admin;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.dto.CourseAddDto;
import com.atguigu.eduservice.entity.vo.CourserQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Mr.Alan
 * @since 2020-08-16
 */
@RestController
@RequestMapping("/eduService/course")
@CrossOrigin
public class EduCourseController {

    @Resource
    private EduCourseService eduCourseService;

    @ApiModelProperty("通过id获取课程信息")
    @PostMapping("/getInfoById/{id}")
    public R getInfoById(@PathVariable("id") String id) {
        CourseAddDto CourseAddDto = eduCourseService.getInfoById(id);
        if (ObjectUtils.isNotNull(CourseAddDto)) {
            return R.ok().data("form", CourseAddDto);
        }
        return R.error();

    }


    @ApiModelProperty("删除课程")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {

        if (eduCourseService.removeCourseById(id)) {
            return R.ok();
        }
        return R.error();

    }

    @ApiModelProperty("分页查询课程")
    @PostMapping("/page")
    public R page(@RequestBody CourserQuery query) {

        Integer pageNum = query.getPageNum();
        Integer pageSize = query.getPageSize();
        Page<EduCourse> page = new Page<>(pageNum, pageSize);
        eduCourseService.lambdaQuery()
                .like(StringUtils.isNotEmpty(query.getTitle()), EduCourse::getTitle, query.getTitle())
                .eq(!"all".equals(query.getStatus()), EduCourse::getStatus, query.getStatus())
                .between(StringUtils.isNotEmpty(query.getStartTime()) && StringUtils.isNotEmpty(query.getEndTime()),
                        EduCourse::getGmtCreate, query.getStartTime(), query.getEndTime()).page(page);
        return R.ok().data("list", page.getRecords()).data("total", page.getTotal());

    }

    @ApiOperation(value = "添加课程基础信息")
    @PostMapping("/save")
    public R save(@RequestBody CourseAddDto courseAddDto) {

        String courseId = eduCourseService.saveCourseInfo(courseAddDto);
        if (StringUtils.isNotEmpty(courseId)) {
            return R.ok().data("courseId", courseId);
        }
        return R.error();

    }

    @ApiOperation(value = "更新课程基础信息")
    @PostMapping("/update")
    public R update(@RequestBody CourseAddDto courseAddDto) {

        String courseId = eduCourseService.updateCourseInfo(courseAddDto);
        if (StringUtils.isNotEmpty(courseId)) {
            return R.ok().data("courseId", courseId);
        }
        return R.error();

    }

    @ApiModelProperty("发布课程")
    @PostMapping("/confirmRelease/{id}")
    public R confirmRelease(@PathVariable("id") String id) {

        EduCourse course = eduCourseService.lambdaQuery().eq(EduCourse::getId, id).select(EduCourse::getId).one();
        course.setStatus("Normal");
        if (eduCourseService.updateById(course)) {
            return R.ok();
        }
        return R.error();
    }

    @ApiModelProperty("发布课程")
    @PostMapping("/changeStatus/{id}")
    public R changeStatus(@PathVariable("id") String id) {

        EduCourse course = eduCourseService.lambdaQuery().eq(EduCourse::getId, id).select(EduCourse::getId, EduCourse::getStatus).one();
        String status = course.getStatus().equals("Draft") ? "Normal" : "Draft";
        course.setStatus(status);
        if (eduCourseService.updateById(course)) {
            return R.ok();
        }
        return R.error();
    }

}

